package messages;

import java.util.List;

public class SingleTuneRsp extends MessageHeader {

	public static final short	SINGLETUNE_RSP_ID	= 16;

	private short				tuneID;
	private ParListElement[]	msgIDs;
	private short[]				mpbHDs;
	private byte[]				recLens;
	private byte[]				recIDs;
	private List<Short>[]		words;

	/* ---------------------------------------------------------------- CONSTRUCTORS */

	/**
	 * @param _destID
	 * @param _srcID
	 * @param _resp
	 */
	public SingleTuneRsp( short _destID , short _srcID , short _tuneID , ParListElement[] _msgIDs ,
			short[] _mpbHds , byte[] _recLens , byte[] _recIDs , List<Short>[] _words ) {
		super( _destID , _srcID );
		concatCommandID( SINGLETUNE_RSP_ID );
		concatTuneID( _tuneID );
		concatMsgID( _msgIDs );
		concatMpbHd( _mpbHds );
		concatRecLen( _recLens );
		concatRecID( _recIDs ); // transfer complete tuned message
		concatWords( _words );
	}

	/* ---------------------------------------------------------------- PRIVATE */

	/**
	 * @param commandID
	 */
	private void concatCommandID( short commandID ) {
		this.incrementLength( (short) 2 );
	}

	/**
	 * @param _tuneID
	 */
	private void concatTuneID( short _tuneID ) {
		this.tuneID = _tuneID;
		this.incrementLength( (short) 2 );
	}

	/**
	 * @param _msgID
	 */
	private void concatMsgID( ParListElement[] _parListElements ) {
		this.msgIDs = _parListElements;
		if ( _parListElements != null ) 
			this.incrementLength( (short) ( ( _parListElements.length * ParListElement.PAR_LIST_ELEMENT_SIZE ) ) );
	}

	/**
	 * @param _mpbHd
	 */
	private void concatMpbHd( short[] _mpbHds ) {
		this.mpbHDs = _mpbHds;
		if ( _mpbHds != null ) this.incrementLength( (short) ( _mpbHds.length * 2 ) );
	}

	/**
	 * @param _words
	 */
	private void concatWords( List<Short>[] _words ) {
		this.words = _words;
		if ( _words != null ) {
			int size = 0;
			for ( List<Short> l : _words )
				size += l.size( );
			this.incrementLength( (short) size );
		}
	}

	/**
	 * @param _recID
	 */
	private void concatRecID( byte[] _recIDs ) {
		this.recIDs = _recIDs;
		if ( _recIDs != null ) this.incrementLength( (short) _recIDs.length );

	}

	/**
	 * @param _recLen
	 */
	private void concatRecLen( byte[] _recLens ) {
		this.recLens = _recLens;
		if ( _recLens != null ) this.incrementLength( (short) _recLens.length );
	}

	/* ---------------------------------------------------------------- PUBLIC */

	public short getTuneID( ) {
		return tuneID;
	}

	public void setTuneID( short tuneID ) {
		this.tuneID = tuneID;
	}

	public ParListElement[] getMsgIDs( ) {
		return msgIDs;
	}

	public void setMsgIDs( ParListElement[] msgIDs ) {
		this.msgIDs = msgIDs;
	}

	public byte[] getRecLens( ) {
		return recLens;
	}

	public void setRecLens( byte[] recLens ) {
		this.recLens = recLens;
	}

	public byte[] getRecIDs( ) {
		return recIDs;
	}

	public void setRecIDs( byte[] recIDs ) {
		this.recIDs = recIDs;
	}

	public short[] getMpbHDs( ) {
		return mpbHDs;
	}

	public void setMpbHDs( short[] mpbHDs ) {
		this.mpbHDs = mpbHDs;
	}

	public void setWords( List<Short>[] words ) {
		this.words = words;
	}

	public List<Short>[] getWords( ) {
		return words;
	}

	@Override
	public String toString( ) {
		StringBuilder stb = new StringBuilder( );
		stb.append( " [ SINGLE_TUNE_RSP ] = " + super.toString( ) + " | <Command_ID="
				+ SINGLETUNE_RSP_ID + "> | <TuneID=" + this.tuneID + "> | " );

		if ( this.msgIDs == null || this.mpbHDs == null || this.recIDs == null
				|| this.recLens == null )
			stb.append( " <null> " );
		else {
			stb.append( " [" );
			for ( int i = 0 ; i < msgIDs.length ; i++ ) {
				stb.append( "[ "+this.msgIDs[i]+" | <MbpHd=" + this.mpbHDs[i]
						+ "> | <app_rec_len=" + recLens[i] + ".recID=" + this.recIDs[i] + "> ]" );

				if ( this.words == null )
					stb.append( " <null> " );
				else {
					stb.append( " [" );
					for ( int k = 0 ; k < words[i].size( ) ; k++ ) {
						stb.append( " <Word=" + words[i].get( k ) + "> " );
					}
					stb.append( "] " );
				}
			}
			stb.append( "] " );
		}

		return stb.toString( );
	}
}
